﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sc.Service;
using System.Web.Script.Serialization;
using Sc.Domain;

namespace Sc.Web.Controllers
{
    public class BaseItemController : BaseController
    {
        /// <summary>
        /// 列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.BaseItemType = new BaseItemService().GetBaseItemType();
            return View();
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="baseItemTypeId">分类ID</param>
        /// <returns></returns>
        public JsonResult GetBaseItem(int baseItemTypeId)
        {
            var list = new BaseItemService().GetList();
            if (baseItemTypeId != 0)
            {
                list = list.Where(it => it.BaseItemTypeId == baseItemTypeId).ToList();
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        public int SaveBaseItem()
        {
            var data = Request.Params["data"];
            JavaScriptSerializer json = new JavaScriptSerializer();
            var ent = json.Deserialize<BaseItem>(data);
            var tag = 0;
            if (ent.Id == 0)
            {
                tag = new BaseItemService().Add(ent);
            }
            else
            {
                tag = new BaseItemService().Update(ent);
            }
            return tag;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">删除的ID</param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            return new BaseItemService().DeleteList(ids) ? 1 : 0; 
        }
	}
}